package com.example.lib_audio.audio

import android.util.Log
import com.example.lib_base.model.Song
import kotlin.random.Random

//提供给外部
class AudioController private constructor(){
    private val audioPlayer = AudioPlayer()
    private var songs = arrayListOf<Song>()
    private var currentSong:Song? = null
    private var currentMode = Mode.LOOP
    lateinit var mCallback:(Song)->Unit

    init {
        addCompletionListener {
            nextSong()
        }
    }

    companion object{
        private var instance:AudioController? = null

        fun getInstance():AudioController?{

            if(instance == null){
                synchronized(this){
                    if(instance == null){
                        instance = AudioController()
                    }
                }
            }
            return instance!!
        }
    }

    fun isPlaying() = audioPlayer.isPlaying()

    //停止定时器
    fun stopTimer(){
        audioPlayer.stopTimer()
    }


    //播放
    fun start(song:Song){
        if(audioPlayer.isPlaying()){//是否在播放
            if(currentSong != null){
                if(currentSong!!.author == song.author && currentSong!!.title == song.title){//同一首
                    audioPlayer.replay()
                }else{
                    pause()
                    songs.add(song)
                    currentSong = song
                    audioPlayer.loadMusic(song)
                }
            }
        }else{
            if(!songs.contains(song)) {
                songs.add(song)
            }
            currentSong = song
            audioPlayer.loadMusic(song)
        }

    }

    //下一曲
    fun nextSong(){
        if(songs.size == 1) {
            pause()
            currentSong?.let { start(it) }
            return
        }

        var index = -1
        when(currentMode){
            Mode.LOOP -> {
                if(songs.last() == currentSong){ //最后一首
                    index = 0
                }else{
                    index = songs.indexOf(currentSong)+1
                }

            }
            Mode.RANDOM -> {

                while (true){
                    index = Random.nextInt(songs.size)
                    val currentIndex = songs.indexOf(currentSong)
                    if(index != currentIndex){
                        break
                    }
                }

            }
            Mode.REPEAT -> {
                audioPlayer.replay()
                return
            }
        }

        if(currentMode != Mode.REPEAT) {
            mCallback(songs[index])
        }


        pause()
        start(songs[index])

    }

    //上一曲
    fun previousSong(){
        if(songs.size == 1) {
            pause()
            currentSong?.let { start(it) }
            return
        }

        var index = -1
        when(currentMode){
            Mode.LOOP -> {
                if(songs.first() == currentSong){ //第一首
                    index = songs.lastIndex
                }else{
                    index = songs.indexOf(currentSong)-1
                }

            }
            Mode.RANDOM -> {

                while (true){
                    index = Random.nextInt(songs.size)
                    val currentIndex = songs.indexOf(currentSong)
                    if(index != currentIndex){
                        break
                    }
                }

            }
            Mode.REPEAT -> {
                audioPlayer.replay()
                return
            }
        }

        pause()
        start(songs[index])
    }

    //暂停
    fun pause(){
        audioPlayer.pause()
    }

    //释放
    fun release(){

        audioPlayer.release()
        songs = arrayListOf()
    }

    //恢复
    fun resume(){
        audioPlayer.resume()
    }

    //获取当前播放的音乐
    fun getCurrentPlaySong():Song? = currentSong

    //获取歌曲总时长
    fun getDuration() = audioPlayer.getSongDuration()

    //监听器
    fun addCompletionListener(callback:()->Unit){
        audioPlayer.addCompletionListener {
            callback()
        }
    }

    //暂停还是播放
    fun playOrPause(){
        if(audioPlayer.isPlaying()){
            pause()
        }else{
            resume()
        }
    }


    //播放模式
    enum class Mode{
        LOOP,REPEAT,RANDOM
    }

}



